import {WebEventBus} from "../eventbus/WebEventBus";
import {EventBus} from "../eventbus/EventBus";
import {BasicManager} from "./BasicManager";

class EventBusCollection extends BasicManager {
    constructor() {
        super();
    }

    create(ctx) {
        const webBus = new WebEventBus();
        const nativeBus = window.top === undefined ? window.Bridge : window.top.Bridge;
        const eventBus = new EventBus(webBus, nativeBus);
        this.add(ctx.Key, eventBus);
        return eventBus;
    }

    broadcast(name, data) {
        this.each((key, item) => {
            item.WebEventBus.dispatch(name, data);
        });
    }
}

export const EventBusManager = (function () {
    let instance;
    return {
        name: "EventBusCollection",
        getInstance: function () {
            if (instance === undefined) {
                instance = new EventBusCollection();
            }
            return instance
        }
    }
})();